<#assign tit>商品销售</#assign>
<@page.html title=tit>
<div style="width:880px; float:left;">

  <form id="fm_input" method="post" action="_add" onsubmit="return save(this);">
    <input type="hidden" name="payls" value="">
    <input type="hidden" id="listIndex" value="0">
    <input type="hidden" id="shopCode" name="shopCode" value="${shopCode!}">
    <div class="kbox" border="1">
		<div class="button"><input type="button" class="opwin" value="+添加商品" onclick="selectProduct();"/></div>
    	<div class="lsinfo">销售商品列表</div>
  		<div id="div_list">
		<#if pg??>${pg.countHtml!}</#if>
		<table class="list" id = "testId">
		  <tr>
		    <th width="10%">商品编码</th>
		    <th width="10%">商品名称</th>
		    <th width="10%">单位</th>
		    <th width="10%">标准单价</th>
		    <th width="10%">数量</th>
		    <th width="10%">折扣类别</th>
		    <th width="10%">折扣单价</th>
		    <th width="10%">小计金额</th>
		    <th width="10%">折扣备注</th>
    		<th width="10%">操作</th>
		  </tr>
		  <#if list??><#list list as c>
		  <tr<#if c_index%2==1> class="line2"<#else> class="line1"</#if>>
		    <td>${c.productCode!}</td>
		    <td>${c.productName!}</td>
		    <td><@value type="productUnit" key="${c.unit!}"/></td>
		    <td>${c.price!}</td>
		    <td><input style="text" name="lstProduct[${c_index!}].qty" vtp="number" value="0" size="8" maxlength="16" onblur="changeQty(${c_index!})"></td>
		    <td></td>
		    <td></td>
		    <td><input style="text" value="" size="8" class="totalMoney" maxlength="16" readonly></td>
		    <td></td>
		    <td class="oper"><a class="del" href="javascript:cdel(${c_index!})">删除</a></td>
			<input type="hidden" name="lstProduct[${c_index!}].productCode" value="${c.productCode!}">
		  </tr>
		  </#list></#if>
		</table>
		<#if pg??>${pg.pageHtml!}</#if>
  		</div>
  		<div align="right">
	      <table class="input">
	        <tr>
	          <th width="30%" class="right">合计金额：</th>
	          <td width="70%" class="right"><input type="text" id="money" name="money" value="" class="totalMoney" readonly></td>
	        </tr>
	      </table>
  		</div>
    </div>
    <div class="button"><input type="submit" value="确认并提交"></div>
  </form>
</div>

<script type="text/javascript">
function selectProduct(c) {
  var shopCode = getEl("shopCode").value;
  openWin("getProduct?saleFlg=1&shopCode="+shopCode, 700, 400);
}

function addProduct(productInfo) {
    var tb = getEl("testId");
    if(!tb) return;
    var i, tr, td;
    i=tb.rows.length;
    
    for(k=1; k<tb.rows.length; k++) {
		// 重复校验
    	if(tb.rows[k].cells[0].innerHTML.replace(/<.+?>/gim,'')==productInfo.cells[0].innerHTML.replace(/<.+?>/gim,'')) {
        	window.alert("请不要重复选择该商品！"); 
        	return null;
    	}
    }
    
    var j;
    j = Number(getEl("listIndex").value);
    j = j + 1;
    getEl("listIndex").value = j;
    tr = tb.insertRow(-1);
    tr.className = "line"+((i%2));
    tr.id=i;
    td = tr.insertCell(-1);
    td.innerHTML = productInfo.cells[0].innerHTML.replace(/<.+?>/gim,'') + '<input type="hidden" name="lstProduct[' + j + '].productCode" value='+productInfo.cells[0].innerHTML.replace(/<.+?>/gim,'')+'>';
    td = tr.insertCell(-1);
    td.innerHTML = productInfo.cells[1].innerHTML.replace(/<.+?>/gim,'');
    td = tr.insertCell(-1);
    td.innerHTML = productInfo.cells[2].innerHTML.replace(/<.+?>/gim,'');
    // 单价
    td = tr.insertCell(-1);
    td.innerHTML = productInfo.cells[3].innerHTML.replace(/<.+?>/gim,'') + '<input type="hidden" name="lstProduct[' + j + '].price" value='+productInfo.cells[3].innerHTML.replace(/<.+?>/gim,'')+'>';
    // 数量
    td = tr.insertCell(-1);
    td.innerHTML = '<input style="text" id="lstProduct[' + j + '].qty" name="lstProduct[' + j + '].qty" vtp="number" value="-1" size="8" maxlength="16" onblur="changeQty(' + j + ')">';
    // 折扣类别
    td = tr.insertCell(-1);
    td.innerHTML = '<select id="lstProduct[' + j + '].allowanceType" name="lstProduct[' + j + '].allowanceType" onChange="changePriceType(' + j + ')"><#if allowType??><#list allowType as t><option value="${t.id}">${t.name}</option></#list></#if></select>'
    // 折扣单价
    td = tr.insertCell(-1);
    td.innerHTML = '<input style="text" id="lstProduct[' + j + '].allowancePrice" name="lstProduct[' + j + '].allowancePrice" value=' + productInfo.cells[3].innerHTML.replace(/<.+?>/gim,'') + ' size="8" readonly="readonly">';
    // 小计金额
    td = tr.insertCell(-1);
    td.innerHTML = '<input style="text" value="" size="8" class="totalMoney" maxlength="16" readonly>';
    // 折扣备注
    td = tr.insertCell(-1);
    td.innerHTML = '<input style="text" name="lstProduct[' + j + '].memo" value="" size="16">';

    td = tr.insertCell(-1);
    td.className = "oper"
    td.innerHTML = '<a class="del" href="javascript:void(0)" onclick="cdel(this)">删除</a>'
    				+ '<input type="hidden" id="lstProduct[' + j + '].allowancePrice0" name="lstProduct[' + j + '].allowancePrice0" value='+productInfo.cells[3].innerHTML.replace(/<.+?>/gim,'')+'>'
    				+ '<input type="hidden" id="lstProduct[' + j + '].allowancePrice1" name="lstProduct[' + j + '].allowancePrice1" value='+productInfo.cells[4].innerHTML.replace(/<.+?>/gim,'')+'>'
    				+ '<input type="hidden" id="lstProduct[' + j + '].allowancePrice2" name="lstProduct[' + j + '].allowancePrice2" value='+productInfo.cells[5].innerHTML.replace(/<.+?>/gim,'')+'>'
    				+ '<input type="hidden" id="lstProduct[' + j + '].allowancePrice3" name="lstProduct[' + j + '].allowancePrice3" value='+productInfo.cells[6].innerHTML.replace(/<.+?>/gim,'')+'>'
    				+ '<input type="hidden" id="lstProduct[' + j + '].allowancePrice4" name="lstProduct[' + j + '].allowancePrice4" value='+productInfo.cells[7].innerHTML.replace(/<.+?>/gim,'')+'>'
    				+ '<input type="hidden" id="lstProduct[' + j + '].allowancePrice5" name="lstProduct[' + j + '].allowancePrice5" value='+productInfo.cells[8].innerHTML.replace(/<.+?>/gim,'')+'>'
    				+ '<input type="hidden" id="lstProduct[' + j + '].quantity" name="lstProduct[' + j + '].quantity" value='+productInfo.cells[9].innerHTML.replace(/<.+?>/gim,'')+'>'
    				;
    
    calcMoney();
  }

function calcMoney() {
    var tb = getEl("testId");
    if(!tb) return;
    var i;
    i=tb.rows.length;
    var totalMoney = 0;
    for(i=1; i<tb.rows.length; i++) {
    	var money = 0;
		// 数字校验
    	if(!chkValueType(tb.rows[i].cells[4].firstChild.value, 'number')) {
        	try {tb.rows[i].cells[4].firstChild.focus();} catch(e) {}
        	return null;
    	}
    	// 小计金额的计算
    	money = tb.rows[i].cells[6].firstChild.value * tb.rows[i].cells[4].firstChild.value;
    	tb.rows[i].cells[7].firstChild.value = money;
    	// 合计金额的计算
    	totalMoney = totalMoney + money;
    }
    document.getElementById("money").value = totalMoney;
}

// 数量输入后check
function changeQty(j) {

    var qty = getEl("lstProduct[" + j + "].qty");
	// 数字校验
    if(!chkValueType(qty.value, 'number')) {
        try {qty.focus();} catch(e) {}
        return null;
    }
    // 库存校验
    var stock = getEl("lstProduct[" + j + "].quantity");
	if (Number(stock.value) < Number(qty.value)) {
      	window.alert("库存不足，请确认！"); 
		try {qty.focus();} catch(e) {}
      	return false;
	}

	// 重新计算金额
    calcMoney();
}

// 折扣类型输入后check
function changePriceType(j) {

    var type = getEl("lstProduct[" + j + "].allowanceType");
    var price = getEl("lstProduct[" + j + "].allowancePrice");
	if (type.value=="01") {
		if (getEl("lstProduct[" + j + "].allowancePrice0").value == "") {
      		window.alert("没有设置这种价格，请确认"); 
      		price.value = "";
		} else {
			price.value = getEl("lstProduct[" + j + "].allowancePrice0").value;
		}
	}
	if (type.value=="02") {
		if (getEl("lstProduct[" + j + "].allowancePrice1").value == "") {
      		window.alert("没有设置这种价格，请确认"); 
      		price.value = "";
      		return false;
		} else {
			price.value = getEl("lstProduct[" + j + "].allowancePrice1").value;
		}
	}
	if (type.value=="03") {
		if (getEl("lstProduct[" + j + "].allowancePrice2").value == "") {
      		window.alert("没有设置这种价格，请确认"); 
      		price.value = "";
		} else {
			price.value = getEl("lstProduct[" + j + "].allowancePrice2").value;
		}
	}
	if (type.value=="04") {
		if (getEl("lstProduct[" + j + "].allowancePrice3").value == "") {
      		window.alert("没有设置这种价格，请确认"); 
      		price.value = "";
		} else {
			price.value = getEl("lstProduct[" + j + "].allowancePrice3").value;
		}
	}
	if (type.value=="05") {
		if (getEl("lstProduct[" + j + "].allowancePrice4").value == "") {
      		window.alert("没有设置这种价格，请确认"); 
      		price.value = "";
		} else {
			price.value = getEl("lstProduct[" + j + "].allowancePrice4").value;
		}
	}
	if (type.value=="06") {
		if (getEl("lstProduct[" + j + "].allowancePrice5").value == "") {
      		window.alert("没有设置这种价格，请确认"); 
      		price.value = "";
		} else {
			price.value = getEl("lstProduct[" + j + "].allowancePrice5").value;
		}
	}
	// 重新计算金额
    calcMoney();
}

function cdel(src) {
    var tb = getEl("testId");
    if(!tb) return;
	var tr=document.getElementById(src.parentElement.parentElement.id);
	tr.parentNode.removeChild(tr); 
    calcMoney();
}

function save(fm) {
    var tb = getEl("testId");
    if(!tb) return;
    var i;
    i=tb.rows.length;
    if(i==1) {
      window.alert("没有选择销售的商品"); 
      return false;
    }
    if(document.getElementById("money").value==0) {
      window.alert("没有选择销售的商品"); 
      return false;
    }
    
    for(i=1; i<tb.rows.length; i++) {
		// 员工价的时候
    	if(tb.rows[i].cells[5].firstChild.value == "02" && tb.rows[i].cells[8].firstChild.value == "") {
        	try {tb.rows[i].cells[8].firstChild.focus();} catch(e) {}
        	window.alert("请在折扣备注中输入员工工号"); 
        	return false;
    	}
		// 员工价的时候
    	if(tb.rows[i].cells[5].firstChild.value == "03" && tb.rows[i].cells[8].firstChild.value == "") {
        	try {tb.rows[i].cells[8].firstChild.focus();} catch(e) {}
        	window.alert("请在折扣备注中输入会员卡卡号"); 
        	return false;
    	}
		// 价格为空的时候
    	if(tb.rows[i].cells[6].firstChild.value == "") {
        	try {tb.rows[i].cells[6].firstChild.focus();} catch(e) {}
        	window.alert("请输入折扣单价"); 
        	return false;
    	}
    }
    
        
    if(!fm) fm = getEl("fm_input");

    var mm = fm.money.value;
    post(fm, function(){
      		openWin("getPay?money="+mm, 500, 400);
      		return false;
    });
    return false;
}

function payOk(vv) {
    var fm = getEl("fm_input");
    fm.payls.value = vv;
    postForm("_add", fm, saveRet);
  }

function saveRet(data) {
    window.alert(data.msg);
    if(data.ret) {
    	/*getEl("fm_input").reset();
    	var tb = getEl("testId");
    	if(!tb) return;
    	var rowNum = tb.rows.length;
    	for (i=1;i<rowNum;i++){
         	tb.deleteRow(i);
         	rowNum=rowNum-1;
         	i=i-1;
     	}*/
      var fm = getEl("fm_input");
      fm.action = "print";
      fm.submit();
    }
}
</script>
</@page.html>